package com.spzx.cart.controller;


import com.spzx.cart.domain.CartItem;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping
@Tag(name = "购物车模块")
public class CartController extends BaseController {

    @Resource
    CartService cartService;

    @Operation(summary = "添加商品到购物车")
    @GetMapping("addToCart/{skuId}/{num}")
    public AjaxResult addToCart(@PathVariable("skuId") Long skuId,@PathVariable("num") Integer num){
        return toAjax(cartService.addToCart(skuId,num));
    }

    @Operation(summary = "查询购物车信息")
    @GetMapping("cartList")
    public AjaxResult cartList(){
        return success(cartService.selectCartList());
    }


    @Operation(summary = "切换购物项选中状态")
    @GetMapping("checkCart/{skuId}/{checkStatus}")
    public AjaxResult checkCart(@PathVariable("skuId") Long skuId,@PathVariable("checkStatus") Integer checkStatus){
        return toAjax(cartService.checkCart(skuId,checkStatus));
    }


    @Operation(summary = "删除指定购物项")
    @DeleteMapping("deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId") Long skuId){
        return AjaxResult.success(cartService.deletedCartByskuId(skuId));
    }

    @Operation(summary = "清空购物车")
    @GetMapping("clearCart")
    public AjaxResult clearCart(){
        return toAjax(cartService.clearCart());
    }
    @Operation(summary = "切换所有购物项选中状态")
    @GetMapping("allCheckCart/{checkStatus}")
    public AjaxResult allCheckCart(@PathVariable("checkStatus")Integer checkStatus){
        return toAjax(cartService.allCheckCart(checkStatus));
    }



    @InnerAuth
    @Operation(summary = "内部调用接口：查询用户已选中的购物项列表")
    @GetMapping("listCheckedCartItems/{userId}")
    public R<List<CartItem>> listCheckedCartItems(@PathVariable("userId")Long userId){
        return R.ok(cartService.selectCheckedCartItems(userId));
    }
}



